﻿using System;
using System.Drawing;
using System.Globalization;
using Business;
using System.Web.UI.WebControls;
using System.Linq;
using System.Collections.ObjectModel;
using Entities;
using System.Web.UI;
namespace ServicesDirectory.UserControl
{
    /// <summary>
    /// 
    /// </summary>
    public partial class ListOrganisations : System.Web.UI.UserControl
    {
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lbtAll.CommandArgument = "All";
                ViewState["btnFilter"] = "lbtAll";
                BindData("All", true);
            }
            cmdBackAll.Click += BackAll_Click;
            cmdBacklist.Click += BacklistClick;
            cmdNext.Click += Next_Click;
            cmdNextAll.Click += NextAll_Click;
            lbtAll.Click += lbtAll_Click;
            lbt09.Click += Lbt09_Click;
            lbtA.Click += LbtA_Click;
            lbtF.Click += LbtF_Click;
            lbtK.Click += LbtK_Click;
            lbtO.Click += LbtO_Click;
            lbtS.Click += LbtS_Click;
            lbtW.Click += LbtW_Click;
            lbtOrderOrgName.Click += lbtOrderOrgName_Click;
            lbtAddress.Click += LbtAddress_Click;
            lbtPostcode.Click += LbtPostcodeClick;
            lbtContact.Click += LbtContact_Click;
            chkInactive.CheckedChanged += ChkInActive_CheckedChanged;

        }

        /// <summary>
        /// Handles the Click event of the LbtContact control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtContact_Click(object sender, EventArgs e)
        {
            OrganisationOrderByContact(string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument);
        }

        /// <summary>
        /// LBTs the postcode click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtPostcodeClick(object sender, EventArgs e)
        {
            OrganisationOrderByPostCode(string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument);
        }

        /// <summary>
        /// Handles the Click event of the LbtAddress control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtAddress_Click(object sender, EventArgs e)
        {
            OrderOrganisationAddress(
                string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument,
                !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the CheckedChanged event of the ChkInActive control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void ChkInActive_CheckedChanged(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            BindData(string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument,
                 !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the lbtOrderOrgName control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtOrderOrgName_Click(object sender, EventArgs e)
        {
            OrderOrganisationName(
                string.IsNullOrEmpty(lbtAll.CommandArgument) ? "All" : lbtAll.CommandArgument,
                !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the ItemDataBound event of the RptOrganisation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RepeaterItemEventArgs" /> instance containing the event data.</param>
        protected void RptOrganisation_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var lblIsActive = e.Item.FindControl("lblIsactive") as Label;
            var lblPostcode = e.Item.FindControl("lblPostcode") as Label;

            var lblContact = e.Item.FindControl("lblContactName") as Label;
            if (lblIsActive != null)
            {
                lblIsActive.Text = lblIsActive.Text == "True" ? "Yes" : "No";

            }
            if (lblPostcode != null)
            {
                if (AddressBusiness.Instance.GetAddress(lblPostcode.Text) != null)
                {
                    lblPostcode.Text = AddressBusiness.Instance.GetAddress(lblPostcode.Text).PostCode;
                    lblPostcode.ToolTip = lblPostcode.Text;
                    lblPostcode.Text = lblPostcode.Text.Length > 23
                                           ? string.Concat(lblPostcode.Text.Substring(0, 20), "...")
                                           : lblPostcode.Text;
                }
            }
            if (lblContact != null)
            {
                string message;
                var contact = ContactBusiness.Instance.GetContact(lblContact.Text, out message);
                if (contact != null)
                {
                    lblContact.Text = contact.FirstName + " " + contact.SurName;
                    lblContact.ToolTip = lblContact.Text;
                    lblContact.Text = lblContact.Text.Length > 15 ? string.Concat(lblContact.Text.Substring(0, 12), "...") : lblContact.Text;
                }
                else
                {
                    lblContact.Text = "";
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the LbtW control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtW_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "wz";
            ViewState["btnFilter"] = "lbtW";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the LbtS control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtS_Click(object sender, EventArgs e)
        {

            lbtAll.CommandArgument = "sv";
            ViewState["btnFilter"] = "lbtS";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the LbtO control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtO_Click(object sender, EventArgs e)
        {

            lbtAll.CommandArgument = "or";
            ViewState["btnFilter"] = "lbtO";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the LbtK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtK_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "kn";
            ViewState["btnFilter"] = "lbtK";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the LbtF control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtF_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "fj";
            ViewState["btnFilter"] = "lbtF";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the LbtA control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void LbtA_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "ae";
            ViewState["btnFilter"] = "lbtA";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the Lbt09 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void Lbt09_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "09";
            ViewState["btnFilter"] = "lbt09";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the lbtAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void lbtAll_Click(object sender, EventArgs e)
        {
            lbtAll.CommandArgument = "All";
            ViewState["btnFilter"] = "lbtAll";
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the NextAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void NextAll_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = lblTotalPage.Text;
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);
        }

        /// <summary>
        /// Handles the Click event of the Next control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void Next_Click(object sender, EventArgs e)
        {
            if ((int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) + 1) <= ((int.Parse(lblTotalPage.Text, CultureInfo.InvariantCulture))))
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) + 1).ToString(CultureInfo.InvariantCulture);
                BindData(lbtAll.CommandArgument, !chkInactive.Checked);


            }
        }



        /// <summary>
        /// Backlists the click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void BacklistClick(object sender, EventArgs e)
        {
            if ((int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) - 1) > 0)
            {
                txtPageNumber.Text = (int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture) - 1).ToString(CultureInfo.InvariantCulture);
                BindData(lbtAll.CommandArgument, !chkInactive.Checked);
            }
        }

        /// <summary>
        /// Handles the Click event of the BackAll control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        void BackAll_Click(object sender, EventArgs e)
        {
            txtPageNumber.Text = "1";
            BindData(lbtAll.CommandArgument, !chkInactive.Checked);


        }

        /// <summary>
        /// Binds the data.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        private void BindData(string keyword, bool isActive)
        {
            if (ViewState["btnFilter"] != null)
            {
                ((LinkButton)FindControl(ViewState["btnFilter"].ToString())).ForeColor = Color.FromName("#99ff00");

            }
            var lstOrg = OrganizationBusiness.Instance.ListOrganisation(keyword, isActive);
            if (lstOrg != null && lstOrg.Count > 0)
            {
                pnNorecord.Visible = false;
                lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                rptOrganisation.DataBind();
            }
            else
            {
                pnNorecord.Visible = true;
                txtPageNumber.Text = "0";
                lblTotalPage.Text = "0";
                rptOrganisation.DataSource = null;
                rptOrganisation.DataBind();
            }

        }
        /// <summary>
        /// Orders the name of the organisation.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        private void OrderOrganisationName(string keyword, bool isActive)
        {
            var lstOrg = OrganizationBusiness.Instance.ListOrganisation(keyword, isActive);
            if (string.IsNullOrEmpty(lbtOrderOrgName.CommandArgument) || lbtOrderOrgName.CommandArgument == "ASC")
            {
                lstOrg = new Collection<Organisation>(lstOrg.OrderBy(c => c.OrgName).ToList());
                lbtOrderOrgName.CommandArgument = "DESC";
            }
            else
            {
                lbtOrderOrgName.CommandArgument = "ASC";
                lstOrg = new Collection<Organisation>(lstOrg.OrderByDescending(c => c.OrgName).ToList());
            }

            if (lstOrg != null && lstOrg.Count > 0)
            {
                lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                rptOrganisation.DataBind();
            }
            else
            {

                pnNorecord.Visible = true;
                txtPageNumber.Text = "0";
                lblTotalPage.Text = "0";
                rptOrganisation.DataSource = null;
                rptOrganisation.DataBind();

            }

        }
        /// <summary>
        /// Organisations the order by contact.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        private void OrganisationOrderByContact(string keyword)
        {

            if (string.IsNullOrEmpty(lbtContact.CommandArgument) || lbtContact.CommandArgument == "ASC")
            {
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByContact(keyword, true);
                if (lstOrg != null && lstOrg.Count > 0)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }

                else
                {
                    pnNorecord.Visible = true;
                    txtPageNumber.Text = "0";
                    lblTotalPage.Text = "0";
                    rptOrganisation.DataSource = null;
                    rptOrganisation.DataBind();

                }
                lbtContact.CommandArgument = "DESC";
            }
            else
            {
                lbtContact.CommandArgument = "ASC";
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByContact(keyword, false);
                if (lstOrg != null && lstOrg.Count > 0)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }
                else
                {
                    pnNorecord.Visible = true;
                    txtPageNumber.Text = "0";
                    lblTotalPage.Text = "0";
                    rptOrganisation.DataSource = null;
                    rptOrganisation.DataBind();
                }
            }

        }
        /// <summary>
        /// Organisations the order by post code.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        private void OrganisationOrderByPostCode(string keyword)
        {

            if (string.IsNullOrEmpty(lbtPostcode.CommandArgument) || lbtPostcode.CommandArgument == "ASC")
            {
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByAddress(keyword, true);
                if (lstOrg != null && lstOrg.Count > 0)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }
                lbtPostcode.CommandArgument = "DESC";
            }
            else
            {
                lbtPostcode.CommandArgument = "ASC";
                var lstOrg = OrganizationBusiness.Instance.OrganisationOrderByAddress(keyword, false);
                if (lstOrg != null && lstOrg.Count > 0)
                {
                    lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                    rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                    rptOrganisation.DataBind();
                }
                else
                {
                    pnNorecord.Visible = true;
                    txtPageNumber.Text = "0";
                    lblTotalPage.Text = "0";
                    rptOrganisation.DataSource = null;
                    rptOrganisation.DataBind();
                }
            }

        }
        /// <summary>
        /// Orders the organisation address.
        /// </summary>
        /// <param name="keyword">The keyword.</param>
        /// <param name="isActive">if set to <c>true</c> [is active].</param>
        private void OrderOrganisationAddress(string keyword, bool isActive)
        {
            var lstOrg = OrganizationBusiness.Instance.ListOrganisation(keyword, isActive);
            if (string.IsNullOrEmpty(lbtAddress.CommandArgument) || lbtAddress.CommandArgument == "ASC")
            {
                lstOrg = new Collection<Organisation>(lstOrg.OrderBy(c => c.AddressLine1).ToList());
                lbtAddress.CommandArgument = "DESC";
            }
            else
            {
                lbtAddress.CommandArgument = "ASC";
                lstOrg = new Collection<Organisation>(lstOrg.OrderByDescending(c => c.AddressLine1).ToList());
            }

            if (lstOrg != null && lstOrg.Count > 0)
            {
                lblTotalPage.Text = lstOrg.Count % 15 == 0 ? (lstOrg.Count / 15).ToString(CultureInfo.InvariantCulture) : (lstOrg.Count / 15 + 1).ToString(CultureInfo.InvariantCulture);
                rptOrganisation.DataSource = OrganizationBusiness.Instance.PagingOrg(int.Parse(txtPageNumber.Text, CultureInfo.InvariantCulture), 15, lstOrg);
                rptOrganisation.DataBind();
            }
            else
            {
                pnNorecord.Visible = true;
                txtPageNumber.Text = "0";
                lblTotalPage.Text = "0";
                rptOrganisation.DataSource = null;
                rptOrganisation.DataBind();
            }

        }
        /// <summary>
        /// RPTs the organisation item command.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="e">The <see cref="RepeaterCommandEventArgs" /> instance containing the event data.</param>
        protected void RptOrganisationItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Detail")
            {
                var lblIsactive = e.Item.FindControl("lblIsactive") as Label;
                if (lblIsactive != null)
                    if (lblIsactive.Text == "Yes")
                    {
                        Response.Redirect("OrganisationDetail.aspx?orgId=" + e.CommandArgument);
                    }
                    else
                    {

                        var script = "var result= window.confirm('Do you want to make this Organization active?'); ";
                        script += "if(result)";
                        script += " location.href='OrganisationDetail.aspx?orgId=" + e.CommandArgument + "&isActive=Yes';";
                        ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(UpdatePanel), UpdatePanel1.ClientID, script, true);
                    }

            }

        }

        /// <summary>
        /// CMDs the create click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
        protected void CmdCreateClick(object sender, EventArgs e)
        {
            Response.Redirect("OrganisationDetail.aspx");
        }


    }
}